<?php

/**
 * contact actions.
 *
 * @package    erp-crm
 * @subpackage contact
 * @author     milwell
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class contactActions extends sfActions
{
  public function executeIndex(){
  }
#get row column
  public function executeGetColumn(sfWebRequest $request){
    $this->contact = ContactPeer::retrieveByPk($request->getParameter('iden'));
    $this->select_url = 'contact/edit';
    $this->setLayout(false);
  }
  
  public function executeGetRow(sfWebRequest $request){
    $this->contact = ContactPeer::retrieveByPk($request->getParameter('iden'));
    $this->select_url = 'contact/editContact';
    $this->setLayout(false);
  }
  
  public function executeSearch(sfWebRequest $request){
    $this->module_title = 'All Contacts';
    $this->myContactSearchForm = new myContactSearchForm();
    $this->search_title = 'Search Contact';
    $this->dialog_mode = $request->getParameter('dialog_mode',0);
  }
    
  public function executeDoSearch(sfWebRequest $request){
    $this->myContactSearchForm = new myContactSearchForm();
    $skey_contact = $request->getParameter('skey_contact');
    $this->myContactSearchForm->bind($skey_contact);
    $this->contacts = $this->myContactSearchForm->search();
    
  }
  
  public function executeCreate(){
    $this->contactForm = new ContactForm();
    $this->iden = myStringGenerator::getIden();
    $this->contactForm->setIden($this->iden);
    $this->setLayout(false);
  }
  
  public function executeEdit(sfWebRequest $request){
    $contact = ContactPeer::retrieveByPk($request->getParameter('iden',$request->getParameter('contact[iden]')));
    $this->forward404Unless($contact);
    $this->contactForm = new ContactForm($contact);
    $this->iden = $contact->getIden();
    $this->setLayout(false);
  }

  public function executeInsert(sfWebRequest $request){
    $this->iden = $request->getParameter('contact[iden]');
    $this->contactForm = new ContactForm();
    $this->contactForm->bind($request->getParameter('contact'));
    if($this->contactForm->isValid()){
      $this->contactForm->save();
      $this->redirect('contact/edit?iden='.$this->contactForm->getObject()->getIden());
    }else
      $this->setTemplate('create');
  }

  public function executeUpdate(sfWebRequest $request){
    $contact_params = $request->getParameter('contact');
    $this->iden=$contact_params['iden'];
    $contact = ContactPeer::retrieveByPk($this->iden);
    if($contact->getStatus()=='INACTIVE'){
      return $this->redirect('contact/edit?iden='.$contact->getIden());
    }
    $this->forward404Unless($contact);
    $this->contactForm = new ContactForm($contact);
    $this->contactForm->bind($contact_params);
    if($this->contactForm->isValid()){
      $this->contactForm->save();
      $this->redirect('contact/edit?iden='.$this->contactForm->getObject()->getIden());
    }
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request){
    $contact = ContactPeer::retrieveByPk($request->getParameter('contact[iden]'));
    $this->message = '';
    $this->iden = false;
    if (! $contact)
      $this->message = 'Contact not found';
    elseif ($contact->hasAccounts())
      $this->message = '"'.$contact->getListName().'" has subaccounts, cannot delete.';
    else{
      $this->iden = $contact->getIden();
      try{
        $contact->delete();
        $this->message = '"'.$contact->getListName().'" deleted';
      }catch (Exception $e){
        $request->getParameterHolder()->set('message',$e->getMessage());
        $this->forward('main','error');
      }
    }
      $this->contact = $contact;
  }

  public function executeUpdateStatus(sfWebRequest $request){
    $this->iden=$request->getParameter('iden');
    $contact = ContactPeer::retrieveByPk($this->iden);
    $this->forward404Unless($contact);
    if($contact->getStatus()=='ACTIVE')
      $contact->setStatus('INACTIVE');
    else
      $contact->setStatus('ACTIVE');
    $contact->save();
    $this->redirect('contact/edit?iden='.$contact->getIden());
  }
  
}
